import originAxios from 'axios';
import { baseUrl } from '@/config/env';

export default function axios(option) {
  return new Promise((resolve, reject) => {
    const instance = originAxios.create({
      baseURL: baseUrl,
      timeout: 50000
    });

    // 响应拦截
    instance.interceptors.response.use(
      response => {
        return response.data;
      },
      error => {
        console.log(error);
        return error.data;
      }
    );
    // 请求拦截
    instance.interceptors.request.use(config => {
      config.headers.Authorization = window.localStorage.getItem('token');
      return config;
    });

    instance(option)
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        reject(err);
      });
  });
}
